package com.tanhua.server.service;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.domain.vo.NearUserVo;
import com.tanhua.domain.vo.UserLocationVo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.UserLocationApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BaiduService {

    @Reference
    private UserLocationApi userLocationApi;
    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity saveLocation(Double longitude, Double latitude, String addrStr) {


        userLocationApi.saveLocation(UserHolder.getUserId(),longitude,latitude,addrStr);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity search(String gender, Integer distance) {
      List<UserLocationVo> userLocationList =  userLocationApi.search(UserHolder.getUserId(),distance);


      List<NearUserVo> list = new ArrayList<>();
        for (UserLocationVo userLocation : userLocationList) {
            Long userId = userLocation.getUserId();
//            1、排除自己
            if(UserHolder.getUserId().longValue() == userId.longValue()){
                continue;
            }

            UserInfo userInfo = userInfoApi.findById(userId);
            if( StringUtils.equals( userInfo.getGender(),gender)){
//            2、控制性别
                NearUserVo nearUserVo = new NearUserVo();
                nearUserVo.setUserId(userInfo.getId());
                nearUserVo.setAvatar(userInfo.getAvatar());
                nearUserVo.setNickname(userInfo.getNickname());
                list.add(nearUserVo);
            }

        }

        return ResponseEntity.ok(list);






    }
}
